package cn.edu.zime.tjh47;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class Class5_1 extends AppCompatActivity implements View.OnClickListener {
    static int rad;
    static final int REQUEST_CODE = 1;
    private static Button jump;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class5_a);
        jump = findViewById(R.id.jump);
        jump.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        Random r = new Random();
        rad = r.nextInt(10);
        if (view.getId() == R.id.jump) {
            EditText text = findViewById(R.id.et);
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("key1", rad);
            bundle.putString("key2", text.getText().toString());
            intent.putExtras(bundle);
            intent.setClass(this, Class5_2.class);
            startActivityForResult(intent, REQUEST_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            Bundle bundle = data.getExtras();
            if (bundle != null) {
                boolean flag = bundle.getBoolean("key3");
                int cnt = bundle.getInt("key2");
                String str = String.format("猜了%d次，%s", cnt, flag ? "猜对了" : "没猜对");
                jump.setText(str);
            }
        }
    }

    public void back(View view) {
        finish();
    }
}
